package com.ruoyi.combat.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.json.JSONUtil;
import com.ruoyi.combat.config.Neo4jConfig;
import com.ruoyi.combat.domain.neo4j.IpNode;
import com.ruoyi.combat.domain.neo4j.IpcNode;
import com.ruoyi.combat.service.ICombatAtlasNewTaskService;
import com.ruoyi.common.utils.StringUtils;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Relationship;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.combat.mapper.CombatAtlasTaskMapper;
import com.ruoyi.combat.domain.CombatAtlasNewTask;

/**
 * 课题四任务Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-04
 */
@Service
public class CombatAtlasNewTaskServiceImpl implements ICombatAtlasNewTaskService {
    @Autowired
    private CombatAtlasTaskMapper combatAtlasTaskMapper;
    @Autowired
    private Neo4jConfig neo4jConfig;

    /**
     * 查询课题四任务
     *
     * @param id 课题四任务主键
     * @return 课题四任务
     */
    @Override
    public CombatAtlasNewTask selectCombatAtlasTaskById(Long id) {
        CombatAtlasNewTask combatAtlasNewTask = combatAtlasTaskMapper.selectCombatAtlasTaskById(id);
        /*if ("2".equals(combatAtlasNewTask.getType()) && "2".equals(combatAtlasNewTask.getStatus())) {
            String after = combatAtlasNewTask.getAfter();
            if ("[]".equals(after)|| StringUtils.isEmpty( after)) {
                String result2 = getResult(combatAtlasNewTask.getIp(), "2");
                combatAtlasNewTask.setAfter(result2);
                combatAtlasTaskMapper.updateCombatAtlasTask(combatAtlasNewTask);
            }
        }*/
        return combatAtlasNewTask;
    }

    public String getResult(String ip, String type) {
        String cql = "MATCH (start_ip:Ip {Ip: '" + ip + "'})-[:BELONGS_TO]->(ipc:IpC) MATCH (ipc)<-[:BELONGS_TO]-(landmark_ip:Ip) WHERE landmark_ip.landmark_node = 1 MATCH path = (landmark_ip)<-[*1..3]-(o:Org) RETURN start_ip, ipc, landmark_ip, o,[node IN nodes(path) | node] as all_nodes_in_path, [rel IN relationships(path) | rel] as all_relationships_in_path, length(path) as path_length";
        Result run = neo4jConfig.run(cql, type);
        List<Record> list = run.list();
        List<Map> listdata = new ArrayList<>();
        list.forEach(record -> {
            List<String> keys = record.keys();
            Map<String, Object> data = new HashMap<>();
            keys.forEach(key -> {
                if ("start_ip".equals(key)) {
                    Node node = record.get(key).asNode();
                    IpNode ipNode = new IpNode();
                    ipNode.setId(node.id());
                    ipNode.setAddress(node.get("Ip").asString());
                    data.put(key, ipNode);
                } else if ("ipc".equals(key)) {
                    Node node = record.get(key).asNode();
                    IpcNode ipcNode = new IpcNode();
                    ipcNode.setId(node.id());
                    ipcNode.setName(node.get("IpC").asString());
                    data.put(key, ipcNode);
                } else if ("landmark_ip".equals(key)) {
                    org.neo4j.driver.Value value = record.get(key);
                    Map map = new HashMap();
                    map.put("id", value.asNode().id());
                    map.put("labels", value.asNode().labels());
                    map.put("value", value.asNode().asMap());
                } else if ("o".equals(key)) {
                    org.neo4j.driver.Value value = record.get(key);
                    Map map = new HashMap();
                    map.put("id", value.asNode().id());
                    map.put("labels", value.asNode().labels());
                    map.put("value", value.asNode().asMap());
                } else if ("all_nodes_in_path".equals(key)) {
                    List<Map> pathList = new ArrayList<>();
                    List<Node> list1 = record.get(key).asList(value -> value.asNode());
                    list1.forEach(node -> {
                        Map map = new HashMap();
                        Iterable<String> labels = node.labels();
                        map.put("labels", labels);
                        map.put("value", node.asMap());
                        map.put("id", node.id());
                        pathList.add(map);
                    });
                    data.put(key, pathList);

                } else if ("all_relationships_in_path".equals(key)) {
                    List<Map> pathList = new ArrayList<>();
                    List<Relationship> list1 = record.get(key).asList(value -> value.asRelationship());
                    list1.forEach(node -> {
                        Map map = new HashMap();
                        map.put("type", node.type());
                        map.put("start", node.startNodeId());
                        map.put("end", node.endNodeId());
                        map.put("value", node.asMap());
                        map.put("id", node.id());
                        pathList.add(map);
                    });
                    data.put(key, pathList);

                } else if ("path_length".equals(key)) {
                    int string = record.get(key).asInt();
                    data.put(key, string);
                }
            });
            listdata.add(data);
        });
        return JSONUtil.toJsonStr(listdata);
    }

    /**
     * 查询课题四任务列表
     *
     * @param combatAtlasTask 课题四任务
     * @return 课题四任务
     */
    @Override
    public List<CombatAtlasNewTask> selectCombatAtlasTaskList(CombatAtlasNewTask combatAtlasTask) {
        return combatAtlasTaskMapper.selectCombatAtlasTaskList(combatAtlasTask);
    }

    /**
     * 新增课题四任务
     *
     * @param combatAtlasTask 课题四任务
     * @return 结果
     */
    @Override
    public int insertCombatAtlasTask(CombatAtlasNewTask combatAtlasTask) {
        return combatAtlasTaskMapper.insertCombatAtlasTask(combatAtlasTask);
    }

    /**
     * 修改课题四任务
     *
     * @param combatAtlasTask 课题四任务
     * @return 结果
     */
    @Override
    public int updateCombatAtlasTask(CombatAtlasNewTask combatAtlasTask) {
        return combatAtlasTaskMapper.updateCombatAtlasTask(combatAtlasTask);
    }

    /**
     * 批量删除课题四任务
     *
     * @param ids 需要删除的课题四任务主键
     * @return 结果
     */
    @Override
    public int deleteCombatAtlasTaskByIds(Long[] ids) {
        return combatAtlasTaskMapper.deleteCombatAtlasTaskByIds(ids);
    }

    /**
     * 删除课题四任务信息
     *
     * @param id 课题四任务主键
     * @return 结果
     */
    @Override
    public int deleteCombatAtlasTaskById(Long id) {
        return combatAtlasTaskMapper.deleteCombatAtlasTaskById(id);
    }
}
